package org.example;

import jakarta.servlet.DispatcherType;
import org.eclipse.jetty.ee10.servlet.FilterHolder;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.example.utils.CommonServlet;
import org.example.filter.LogFilter;

import java.util.EnumSet;

/**
 * 定义一个servlet容器
 */
public class Main {
    public static void main(String[] args) throws Exception {
        //创建服务
        Server server = new Server();
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8080);
        server.addConnector(connector);
        //创建servelt容器 并且添加到容器中
        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        server.setHandler(context);
        ServletHolder servletHolder = context.addServlet(CommonServlet.class, "/*");
        servletHolder.setInitParameter("maxItems", "128");
        //定义filter和范围
        FilterHolder filterHolder = context.addFilter(LogFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        filterHolder.setAsyncSupported(true);
        server.start();
    }
}